from django.http import JsonResponse
from rest_framework.response import Response


class AsyncHttpResult:
    def __init__(self):
        self.code = 0
        self.message = ""
        self.data = None

    @classmethod
    async def success(cls, data=None, message="success", code=200):
        # self.data = data
        # self.code = 1
        # self.message = "success"
        return JsonResponse({
            "code": code,
            "data": data,
            "message": message,
        })

    @classmethod
    async def error(cls, data=None, message="error", code=400):
        return JsonResponse({
            "code": code,
            "data": data,
            "message": message,
        }, status=code)


class HttpResult:
    def __init__(self):
        self.code = 0
        self.message = ""
        self.data = None

    @classmethod
    def success(cls, data=None, message="success", code=200):
        # self.data = data
        # self.code = 1
        # self.message = "success"
        return Response({
            "code": code,
            "data": data,
            "message": message,
        })

    @classmethod
    def error(cls, data=None, message="error", code=400):
        return Response({
            "code": code,
            "data": data,
            "message": message,
        }, status=code)


import json
from datetime import datetime


class WebSocketResult:
    @staticmethod
    def _generate_response(status="success", code=200, message="Operation completed", data=None, type="response"):
        """
        生成标准化的 WebSocket 响应
        :param status: 响应状态 ("success" 或 "error")
        :param code: 状态码 (200、400、500等)
        :param message: 响应消息描述
        :param data: 响应数据内容，默认为空字典
        :return: JSON 格式的响应字符串
        """
        response = {
            "status": status,
            "code": code,
            "message": message,
            "data": data if data is not None else {},
            "timestamp": datetime.utcnow().isoformat() + "Z",
            "type": type
        }
        return json.dumps(response)

    @staticmethod
    def success(message="Operation completed", data=None):
        """
        生成成功响应
        :param message: 成功消息描述
        :param data: 成功数据内容
        :return: JSON 格式的成功响应字符串
        """
        return WebSocketResult._generate_response(status="success", code=200, message=message, data=data,type="response")

    @staticmethod
    def pong():
        """
        响应前端 心跳包
        :param type:
        :return:
        """
        return WebSocketResult._generate_response(status="success", code=200, message="ping_pong", type="ping_pong")

    @staticmethod
    def error(message="An error occurred", code=400, data=None):
        """
        生成错误响应
        :param type:
        :param message: 错误消息描述
        :param code: 错误状态码
        :param data: 错误数据内容
        :return: JSON 格式的错误响应字符串
        """
        return WebSocketResult._generate_response(status="error", code=code, message=message, data=data,type="error")
